# -*- python -*-
#
#  Copyright (c) 2010-2012, Jim Bosch
#  All rights reserved.
#
#  ndarray is distributed under a simple BSD-like license;
#  see the LICENSE file that should be present in the root
#  of the source distribution, or alternately available at:
#  https://github.com/ndarray/ndarray
#
import os
import sys

def RunBinaryUnitTest(target, source, env):
    if not env.Execute(source[0].abspath):
        env.Execute(Touch(target))

def BinaryUnitTest(env, source, dependencies=None):
    if os.name != 'nt':
		bin = env.Program(target="%s.test" % source, source=source)
    else:
        bin = env.Program(source)
    run = env.Command(".%s.succeeded" % str(source), bin, RunBinaryUnitTest)
    if dependencies:
        env.Depends(run, dependencies)
    env.Alias("tests", run)
    return run

def RunPythonUnitTest(target, source, env):
    if not env.Execute('%s %s' % (sys.executable, source[0].abspath)):
        env.Execute(Touch(target))

def PythonUnitTest(env, script, dependencies):
    run = env.Command(".%s.succeeded" % str(script), script, RunPythonUnitTest)
    env.Depends(run, dependencies)
    env.Alias("tests", run)
    return run

Import("env", "testEnv", "pyEnv", "bpEnv")

if testEnv.haveBoostTest:
    BinaryUnitTest(testEnv, "ndarray.cc")
    BinaryUnitTest(testEnv, "views.cc")
    if testEnv.haveEigen:
        BinaryUnitTest(testEnv, "ndarray-eigen.cc")
    if testEnv.haveFFTW:
        fftwEnv = testEnv.Clone()
        BinaryUnitTest(fftwEnv, "ndarray-fft.cc")

if pyEnv.havePython:
    mod = pyEnv.LoadableModule("python_test_mod", "python_test_mod.cc", SHLIBPREFIX="")
    if os.name == 'nt':
        # Move the module to have the correct name.
        mod = env.Command(os.path.join(Dir('.').srcnode().abspath, "python_test_mod.pyd"),
                          mod,
                          Move(os.path.join(Dir('.').srcnode().abspath, "python_test_mod.pyd"), mod[0]))
    PythonUnitTest(pyEnv, "python_test.py", mod)

if bpEnv.haveBoostPython:
    libs = bpEnv.get("LIBS", []) + ["boost_numpy"]
    libpath = bpEnv.get("LIBPATH", []) + [Dir("#Boost.NumPy/libs/numpy/src").abspath]
    rpath = bpEnv.get("RPATH", []) + [Dir("#Boost.NumPy/libs/numpy/src").abspath]
    bpEnv.Append( LINKFLAGS = ["$__RPATH"] )  # workaround for SCons bug #1644
    mod = bpEnv.LoadableModule("bp_test_mod", "bp_test_mod.cc", SHLIBPREFIX="",
                               LIBS=libs, LIBPATH=libpath, RPATH=rpath)
    if os.name == 'nt':
        # Move the module to have the correct name.
        mod = env.Command(os.path.join(Dir('.').srcnode().abspath, "bp_test_mod.pyd"),
                          mod,
                          Move(os.path.join(Dir('.').srcnode().abspath, "bp_test_mod.pyd"), mod[0]))
    PythonUnitTest(bpEnv, "bp_test.py", mod)
    if bpEnv.haveEigen:
        mod = bpEnv.LoadableModule("eigen_bp_test_mod", "eigen_bp_test_mod.cc", SHLIBPREFIX="",
                                   LIBS=libs, LIBPATH=libpath, RPATH=rpath)
        if os.name == 'nt':
            # Move the module to have the correct name.
            mod = env.Command(os.path.join(Dir('.').srcnode().abspath, "eigen_bp_test_mod.pyd"),
                              mod,
                              Move(os.path.join(Dir('.').srcnode().abspath, "eigen_bp_test_mod.pyd"), mod[0]))
        PythonUnitTest(bpEnv, "eigen_bp_test.py", mod)


env.Clean("tests", Glob("*.os"))
env.Clean("tests", Glob("*.pyc"))
env.Clean("tests", Glob("*.so"))
